home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / management / screenmod / source / palette.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  14KB  |  532 lines

  1. /**********************************************************************\
  2.  *             Color Palette Adjuster v2.0 enhanced                   *
  3.  *                                      *
  4.  *   By Syd L. Bolton, with mods from code by Carolyn Schepper, CBM   *
  5.  *          ©1990,91 Legendary Design Technologies, Inc.              *
  6.  *                                      *
  7.  *         Version: 2.0  Date: May 17, 1990  Time: 10:30 pm           *
  8.  *         Version: 2.1  Date: May 2, 1991   Time: 08:12 pm           *
  9.  *                       NOTE: SIMPLY REMOVED SPREAD ROUTINE          *
  10. \**********************************************************************/
  11.  
  12. #define MAXCOLORS 32
  13. #define RGB_BODY  0xFFF
  14.  
  15. #define PWIDE 192         /* Palette Window Width */
  16. #define PBASEHIGH 87      /* Height before adjust for color rows */
  17.  
  18. /* Gadget ID's */
  19. #define colID  1
  20. #define okID   MAXCOLORS + 1
  21. #define resID  MAXCOLORS + 2
  22. #define canID  MAXCOLORS + 3
  23. #define rID    MAXCOLORS + 4
  24. #define bID    MAXCOLORS + 5
  25. #define gID    MAXCOLORS + 6
  26. #define copID  MAXCOLORS + 8
  27. #define swpID  MAXCOLORS + 9
  28.  
  29. /* Proportional RGB Gadgets */
  30.  
  31. #define PRPX  52
  32. #define PRPY -77  /* Top prop, relative to bottom */
  33. #define PRPW  90
  34. #define PRPH  11
  35.  
  36. struct IntuiText
  37.    rTxt = {1,0,JAM1,-11,2,0,"R",0},
  38.    gTxt = {1,0,JAM1,-11,2,0,"G",0},
  39.    bTxt = {1,0,JAM1,-11,2,0,"B",0};
  40.  
  41. struct Image  rImg, gImg, bImg;
  42.  
  43. struct PropInfo
  44.    rInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0},
  45.    gInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0},
  46.    bInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0};
  47.  
  48. struct Gadget
  49.    bGad = { NULL, PRPX,PRPY+30, PRPW,PRPH,
  50.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  51.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  52.             PROPGADGET,(APTR)&bImg, 0,
  53.             &bTxt, 0,(APTR)&bInf, bID, 0 },   /* Last Gadget */
  54.  
  55.    gGad = { &bGad, PRPX,PRPY+15, PRPW,PRPH,
  56.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  57.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  58.             PROPGADGET,(APTR)&gImg, 0,
  59.             &gTxt, 0,(APTR)&gInf, gID, 0 },
  60.  
  61.    rGad = { &gGad, PRPX,PRPY, PRPW,PRPH,
  62.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  63.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  64.             PROPGADGET,(APTR)&rImg, 0,
  65.             &rTxt, 0,(APTR)&rInf, rID, 0 };
  66.  
  67. /* Text Selection Gadgets */
  68. #define TXTX  8
  69. #define TXTY -32      /* Relative to bottom */
  70. #define TXTW  54
  71. #define TXTH  13
  72. #define NEWY -17      /* also relative to bottom */
  73.  
  74. SHORT  txtBorXY[10] = {0,0, TXTW-1,0, TXTW-1,TXTH-1, 0,TXTH-1, 0,0};
  75. struct Border txtBor = { 0,0,3,0,JAM1,5,&txtBorXY[0],NULL };
  76.  
  77. struct IntuiText canTxt = {1,0,JAM1,4,3,0,"CANCEL",0};
  78. struct Gadget canGad = {
  79.    &rGad,   TXTX+122,NEWY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  80.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  81.    &canTxt, 0,0, canID, 0 };      /* Linked to red Prop Gadget */
  82.  
  83. struct IntuiText resTxt = {1,0,JAM1,8,3,0,"RESET",0};
  84. struct Gadget resGad = {
  85.    &canGad, TXTX,TXTY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  86.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  87.    &resTxt, 0,0, resID, 0 };
  88.  
  89. struct IntuiText swpTxt = {1,0,JAM1,12,3,0,"SWAP",0};
  90. struct Gadget swpGad = {
  91.    &resGad, TXTX+122,TXTY,TXTW,TXTH,GADGHCOMP|GRELBOTTOM,
  92.    RELVERIFY, BOOLGADGET, (APTR)&txtBor,0,
  93.    &swpTxt, 0, 0, swpID, 0};
  94.  
  95. struct IntuiText copTxt = {1,0,JAM1,12,3,0,"COPY",0};
  96. struct Gadget copGad = {
  97.    &swpGad, TXTX+61, TXTY,TXTW,TXTH,GADGHCOMP|GRELBOTTOM,
  98.    RELVERIFY, BOOLGADGET, (APTR)&txtBor,0,
  99.    &copTxt, 0, 0, copID, 0};
  100.  
  101. struct IntuiText okTxt = {1,0,JAM1,19,3,0,"OK",0};
  102. struct Gadget okGad = {
  103.    &copGad, TXTX,NEWY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  104.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  105.    &okTxt, 0,0, okID, 0 };  /* Color gads will be linked to this one */
  106.  
  107.  
  108. /* Palette Color Gadgets */
  109.  
  110. #define COLX  10     /* Offsets of first color gadget */
  111. #define COLY  14
  112. #define COLSW  4     /* Spacing between color gadgets */
  113. #define COLSH  4
  114. #define COLW8 18     /* Image width if 8 or more colors */
  115. #define COLW4 (COLW8+COLW8+COLSW)  /* 4 colors */
  116. #define COLW2 (COLW4+COLW4+COLSW)  /* 2 colors */
  117. #define COLH  10     /* Image height */
  118. #define COLDY (COLH+COLSH)
  119. SHORT   colW, colDX;
  120.  
  121. /* Drawn with DrawBorder() when color is selected */
  122. /* Note - the border coords will be filled in by initColGads() */
  123. SHORT  colBorXY[10] = {-2,-2, NULL,-2, NULL,COLH+1, -2,COLH+1, -2,-2};
  124. struct Border colBor = { 0,0,1,0,JAM1,5,&colBorXY[0],NULL };
  125.  
  126. SHORT colGadX[MAXCOLORS], colGadY[MAXCOLORS];
  127. struct Image  *colImgs;
  128. struct Gadget *colGads;
  129.  
  130.  
  131. /* Palette Window */ 
  132. struct NewWindow newpWin = {
  133.    224,24, PWIDE,NULL,   /* Height set before opening */
  134.    2,1,
  135.    MOUSEMOVE | GADGETUP | GADGETDOWN,             /* IDCMP Flags */
  136.    ACTIVATE | SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH, /* Flags */
  137.    NULL,             /* First Gadget - set before OpenWindow */
  138.    NULL,             /* CheckMark */
  139.    "Color Palette",   /* Title */
  140.    NULL,             /* Set up screen pointer before opening */
  141.    NULL,             /* No superbitmap */
  142.    0,0,0,0,          /* Max and Min (no sizing) */
  143.    NULL              /* Screen type - set before opening */
  144.    };
  145.  
  146.  
  147. struct Window   *pWin;      /* Palette window pointer */
  148. struct ViewPort *pVp;
  149. struct RastPort *pRp;
  150. struct IntuiMessage *pMsg;
  151. struct Gadget   *pMGad;
  152. int    pDepth, pColors, pReg, pTopSize;
  153. ULONG  pMClass, colGadMem, colGadMemSize;
  154. UWORD  curColor[MAXCOLORS], resColor[MAXCOLORS];
  155. USHORT pMGadID;
  156. BOOL Done;
  157.  
  158. /* 
  159.  * doPalette(screen)
  160.  */
  161.  
  162. doPalette(pScr)
  163. struct Screen *pScr;
  164.    {
  165.    int k;
  166.  
  167.    /* For safe pcleanup */
  168.    pWin    = NULL;
  169.    colGadMem = NULL;
  170.  
  171.    /* From the screen - needed by Allocations */
  172.    pVp = &pScr->ViewPort;
  173.    pDepth = pScr->RastPort.BitMap->Depth;
  174.    if (pDepth > 5) pDepth=5;
  175.    pColors = 1 << pDepth;
  176.  
  177.    /* Allocations - Note: Requires initialized pDepth, pColors ! */
  178.    colGadMemSize =
  179.       (sizeof(struct Gadget)<<pDepth)+(sizeof(struct Image)<<pDepth);
  180.    if(!(colGadMem=(ULONG)AllocMem(colGadMemSize,MEMF_PUBLIC|MEMF_CLEAR)))
  181.       {
  182.       pcleanup("Can't alloc gadget mem\n");
  183.       return(0);
  184.       }
  185.  
  186.    colGads = (struct Gadget *)colGadMem;
  187.    colImgs = (struct Image *)(colGadMem+(sizeof(struct Gadget)<<pDepth));
  188.  
  189.    /* Init Color Gadgets, linking last one to okGad */
  190.    initColGads(colGads,colImgs,pColors,&okGad);
  191.  
  192.    newpWin.FirstGadget = colGads;
  193.    newpWin.Screen = pScr;
  194.    newpWin.Type = pScr->Flags & 0x000f;
  195.    /* Color rows * color gad Y spacing + 10 for top border */
  196.    pTopSize = ((((pColors-1) >> 3)+1) * COLDY)+ 10;
  197.    newpWin.Height = PBASEHIGH + pTopSize;
  198.    newpWin.TopEdge=(pScr->Height-PBASEHIGH)/2;
  199.    newpWin.LeftEdge=(pScr->Width-PWIDE)/2;
  200.  
  201.    if (!(pWin = (struct Window *)OpenWindow(&newpWin)))
  202.       {
  203.       pcleanup("Can't open palette window\n");
  204.       return(0);
  205.       }
  206.    pRp = pWin->RPort;
  207.  
  208.    /* Get current colors and save for RESET */
  209.    for (k=0; k < pColors; k++)
  210.       curColor[k] = resColor[k] = GetRGB4(pVp->ColorMap,k);
  211.  
  212.    pReg = 0;  /* initially select color 0 */
  213.    selectColor(0, pReg);
  214.  
  215.    Done = FALSE;
  216.    while(! Done)
  217.       {
  218.       Wait(1<<pWin->UserPort->mp_SigBit);
  219.  
  220.       while (pMsg=(struct IntuiMessage *)GetMsg(pWin->UserPort))
  221.          {
  222.          /*  Note that rbg values are modified in 3 cases:
  223.           *     GADGETDOWN of the props
  224.           *     GADGETUP   of the props
  225.           *     MOUSEMOVE (reporting turned on by the props)
  226.           */
  227.  
  228.          pMClass  = pMsg->Class;
  229.          pMGad   = (struct Gadget *)pMsg->IAddress;
  230.          pMGadID = pMGad->GadgetID;
  231.  
  232.          ReplyMsg(pMsg);
  233.  
  234.          switch(pMClass)
  235.             {
  236.             case MOUSEMOVE:
  237.                modColor(pReg);
  238.                break;
  239.  
  240.             case GADGETDOWN:
  241.                switch(pMGadID)
  242.                   {
  243.                   case rID:  case gID:  case bID:
  244.                      modColor(pReg);
  245.                      break;
  246.  
  247.                   default:
  248.                      if ((pMGadID >= colID)&&(pMGadID<(colID+pColors)))
  249.                         {
  250.                         k = pReg;
  251.                         pReg = pMGadID - colID;
  252.                         selectColor(k,pReg);
  253.                         }
  254.                      break;
  255.                   }
  256.  
  257.             case GADGETUP:
  258.                switch (pMGadID)
  259.                   {
  260.